<?php

namespace App\Http\Controllers\Managea;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\PreferRepository;
use App\Repositories\PreferlogRepository;
use App\Repositories\UsersRepository;
use App\Traits\BaseFun;
use DB;

class PreferController extends Controller
{
	public function __construct(PreferRepository $PreferRepository,PreferlogRepository $PreferlogRepository,UsersRepository $UsersRepository)
    {
        $this->PreferRepository=$PreferRepository;
        $this->PreferlogRepository=$PreferlogRepository;
        $this->UsersRepository=$UsersRepository;
    }
    public function Prefer(Request $request)
    {   
        $where=[];
        if ($request->search) {
            $where=['search'=>$request->search];
        }
        $res = $this->PreferRepository->pageList($where);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function PreferAdd(Request $request)
    {   
        $res = $this->PreferRepository->create($request->all());
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '添加失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function PreferEdit(Request $request,$id)
    {
        $res=$this->PreferRepository->info(['id'=>$id]);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function PreferUpdate(Request $request)
    {
        $res=$this->PreferRepository->update($request->all());
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '修改成功!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function PreferDel(Request $request,$id)
    {
        $res=$this->PreferRepository->delete($id);
        $info=$this->PreferlogRepository->info(['preferid'=>$id]);
        if ($info) {
           $this->PreferlogRepository->delete($id);//删除优惠券记录里面数据
        }
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '删除失败或没有此数据!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function PreferFa(Request $request)
    {   
        $count=[];
        if ($request->user_state==1) {
            $userList=$this->UsersRepository->list();
            foreach ($userList as $key => $value) {
                $count[]=array('userid'=>$value['id'],'user_name'=>$value['nickname'],'preferid'=>$request->preferid,'created_at'=>date('Y-m-d H:i:s',time()),'updated_at'=>date('Y-m-d H:i:s',time()));
            }
            $res=DB::table('preferlog')->insert($count);//批量添加
        }else{
            $aid=explode(',', $request->userid);
            $userList=$this->UsersRepository->listIn($aid);
            foreach ($userList as $key => $value) {
                $count[]=array('userid'=>$value['id'],'user_name'=>$value['nickname'],'preferid'=>$request->preferid,'created_at'=>date('Y-m-d H:i:s',time()),'updated_at'=>date('Y-m-d H:i:s',time()));
            }
            $res=DB::table('preferlog')->insert($count);//批量添加
        }
        $this->PreferRepository->update(['id'=>$request->preferid,'user_state'=>$request->user_state,'state'=>2,'adminname'=>$request->adminName]);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '删除失败或没有此数据!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
   

}
